package com.ctrip.xpipe.redis.core.redis.rdb.parser;

import com.ctrip.xpipe.AbstractTest;
import com.ctrip.xpipe.redis.core.redis.rdb.RdbParseContext;
import com.ctrip.xpipe.tuple.Pair;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * @author lishanglin
 * date 2022/6/4
 */
public class RdbAuxParserTest extends AbstractTest {

    private RdbAuxParser rdbAuxParser;

    private byte[] auxGtid = new byte[] {0x04, 0x67, 0x74, 0x69, 0x64, 0x40, (byte)0x84, 0x63, 0x63, 0x64, 0x37, 0x38,
            0x65, 0x64, 0x30, 0x38, 0x39, 0x38, 0x36, 0x65, 0x38, 0x38, 0x34, 0x33, 0x62, 0x38, 0x37, 0x32,
            0x30, 0x33, 0x61, 0x37, 0x32, 0x38, 0x62, 0x32, 0x30, 0x35, 0x64, 0x34, 0x35, 0x37, 0x33, 0x34,
            0x34, 0x34, 0x62, 0x3a, 0x31, 0x2d, 0x33, 0x2c, 0x62, 0x62, 0x30, 0x64, 0x39, 0x62, 0x65, 0x37,
            0x38, 0x31, 0x36, 0x61, 0x64, 0x66, 0x65, 0x39, 0x66, 0x61, 0x32, 0x39, 0x64, 0x30, 0x37, 0x34,
            0x37, 0x62, 0x30, 0x30, 0x32, 0x34, 0x62, 0x32, 0x61, 0x33, 0x34, 0x30, 0x30, 0x62, 0x39, 0x38,
            0x3a, 0x31, 0x2c, 0x30, 0x62, 0x62, 0x62, 0x35, 0x32, 0x61, 0x61, 0x30, 0x36, 0x36, 0x66, 0x65,
            0x62, 0x35, 0x61, 0x65, 0x36, 0x62, 0x62, 0x34, 0x65, 0x33, 0x30, 0x62, 0x64, 0x35, 0x36, 0x61,
            0x61, 0x34, 0x66, 0x38, 0x61, 0x32, 0x30, 0x31, 0x36, 0x61, 0x63, 0x3a, 0x31, 0x2d, 0x34};

    @Before
    public void setupRdbAuxParserTest() {
        RdbParseContext context = new DefaultRdbParseContext();
        rdbAuxParser = new RdbAuxParser(context);
    }

    @Test
    public void testParseAux() {
        ByteBuf byteBuf = Unpooled.wrappedBuffer(auxGtid);
        Pair<String, String> aux = null;

        while (null == aux) {
            aux = rdbAuxParser.read(byteBuf);
        }

        Assert.assertEquals("gtid", aux.getKey());
        Assert.assertEquals("ccd78ed08986e8843b87203a728b205d4573444b:1-3," +
                "bb0d9be7816adfe9fa29d0747b0024b2a3400b98:1," +
                "0bbb52aa066feb5ae6bb4e30bd56aa4f8a2016ac:1-4", aux.getValue());
    }

}
